package com.translate.commons.bean;

import com.translate.commons.enums.TransType;
import com.translate.commons.enums.TranslateFieldType;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 翻译信息
 * 原始数据，字段信息，翻译规则，字典规则的定义
 * Trans开头的都是给翻译用的
 */
@Data
public class TransItemBean {

    public TransItemBean(Object sourceData, Object targetData, Object oldData) {
        this.sourceData = sourceData;
        this.targetData = targetData;
        this.oldData = oldData;
    }

    private Object sourceData;
    private Object targetData;
    private Object oldData;
    private List<TransFieldTypeBean> fieldTypes;

    // 不翻译，置空
    public void addFieldNull(String fieldName) {
        if (fieldName == null || fieldName.trim().length() == 0) {
            return;
        }
        TransFieldTypeBean fieldType = new TransFieldTypeBean();
        fieldType.setFieldName(fieldName);
        fieldType.setType(TransType.NULL);
        addFieldType(fieldType);
    }

    // 独立翻译类型
    public void addField(String fieldName, TransType transType) {
        if (fieldName == null || fieldName.trim().length() == 0) {
            return;
        }
        TransFieldTypeBean fieldType = new TransFieldTypeBean();
        fieldType.setFieldName(fieldName);
        fieldType.setType(transType);
        addFieldType(fieldType);
    }

    // 本地字典+第三方api 翻译
    public void addFieldApi(String fieldName, TranslateFieldType distType) {
        if (fieldName == null || fieldName.trim().length() == 0) {
            return;
        }
        TransFieldTypeBean fieldType = new TransFieldTypeBean();
        fieldType.setFieldName(fieldName);
        fieldType.setType(TransType.API);
        fieldType.setDistType(distType);
        addFieldType(fieldType);
    }

    // 普通字典翻译
    public void addFieldDist(String fieldName, TranslateFieldType distType) {
        if (fieldName == null || fieldName.trim().length() == 0) {
            return;
        }
        TransFieldTypeBean fieldType = new TransFieldTypeBean();
        fieldType.setFieldName(fieldName);
        fieldType.setType(TransType.DIST);
        fieldType.setDistType(distType);
        addFieldType(fieldType);
    }

    private void addFieldType(TransFieldTypeBean fieldType) {
        if (fieldType == null) {
            return;
        }
        if (fieldTypes == null) {
            fieldTypes = new ArrayList<TransFieldTypeBean>();
        }
        fieldTypes.add(fieldType);
    }

}
